<?php

namespace app\controller\shop\model\plus\invitationgift;

use app\common\model\plus\invitationgift\InvitationReceive as InvitationReceiveModel;
use app\common\model\plus\invitationgift\Partake as PartakeModel;
use app\common\model\user\User as UserModel;

/**
 * 获取礼品模型
 */
class InvitationReceive extends InvitationReceiveModel
{
    /**
     * 参与记录列表
     */
    public function getList($id, $data)
    {
        $model = $this;
        if ($data['search'] != '') {
            $model = $model->where('user.nickName|user.mobile', 'like', '%' . trim($data['search']) . '%');
        }
        if (isset($data['create_time']) && $data['create_time']) {
            $model = $model->where('receive.create_time', 'between', [strtotime($data['create_time'][0]), strtotime($data['create_time'][1]) + 86399]);
        }
        if (isset($data['type'])) {
            if ($data['type'] == 0) {
                $model = $model->where('reward_type', '=', 0);
            } else {
                $model = $model->where('reward_type', '<>', 0);
            }
        }
        $list = $model->alias('receive')
            ->field('receive.*,nickName')
            ->with(['user', 'invite'])
            ->join('user', 'user.user_id = receive.user_id', 'left')
            ->where('receive.invitation_gift_id', '=', $id)
            ->order('receive.create_time', 'desc')
            ->paginate($data);
        if ($data['type'] == 1) {
            foreach ($list as $item) {
                $item['inviteName'] = "";
                $partakeInfo = (new PartakeModel)->where('invitation_gift_id', '=', $id)->where('partake_id', '=', $item['user_id'])->find();
                if ($partakeInfo) {
                    $userInfo = UserModel::detail($partakeInfo['user_id']);
                    $item['inviteName'] = $userInfo ? $userInfo['nickName'] : '';
                }
            }
        }
        return $list;
    }
}